using Sirenix.OdinInspector.Editor;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;


public class MainEntry : MonoBehaviour
{

    public void Awake()
    {
        GameSystem.Instance.Init(this.transform);
        GameObject.DontDestroyOnLoad(this);
        SystemInit();

        if (SceneManager.GetActiveScene().name == "Init")
        {
            LoginViewController.Instance.Open();
        }

        
    }

    private void SystemInit()
    {
       CombatConfig.Instace.Init();
       GameEvent.DoHitlag += DOHitlag;
       GameEvent.DoRadialBlur += DoRadialBlur;

       GameDefine.Init();
       ViewManager.Instance.Init();
    }

    // Update is called once per frame
    void Update()
    {
        GameTime.DoUpdate();
        ViewManager.Instance.Update();

        if (Input.GetKeyDown(KeyCode.F))
        {
            BagData.Instance.Add(1001, 1);
            BagData.Instance.Add(2001, 3);
            BagData.Instance.Add(3001, 1);
            BagData.Instance.Add(3002, 1);
        }

        if(Input.GetKeyDown(KeyCode.G))
        {
            BagViewController.Instance.Open();
            BagViewController.Instance.view.Show();
        }

        if (Input.GetKeyDown(KeyCode.H))
        {
            BagViewController.Instance.Close();
        }
    }

    Coroutine coroutine_hitlag;
    public void DOHitlag(int frame, bool lerp)
    {
        if (frame > 0 && Time.timeScale == 1)
        {
            if (coroutine_hitlag != null)
            {
                StopCoroutine(coroutine_hitlag);
            }
            coroutine_hitlag = StartCoroutine(Hitlag(frame, lerp));
        }
    }

    Coroutine coroutine;
    IEnumerator Hitlag(int frame, bool lerp)
    {
        for (int i = 0; i < frame; i++)
        {
            Time.timeScale = lerp ? Mathf.Lerp(1, 0, (float)i / frame) : 0;
            yield return new WaitForEndOfFrame();
        }
        Time.timeScale = 1;
        coroutine_hitlag = null;


    }
    RadialBlur radialBlur;
    Volume volume;
    Coroutine RadiaBlurCoroutine;
    private void DoRadialBlur(RadialBlurConfig config)
    {
        if (volume == null)
        {
            volume = GameObject.Find("Sky and Fog Global Volume").GetComponent<Volume>();
            volume.profile.TryGet<RadialBlur>(out radialBlur);
        }
        if (RadiaBlurCoroutine != null)
        {
            StopCoroutine(RadiaBlurCoroutine);
        }
        RadiaBlurCoroutine = StartCoroutine(RadialBlur(config));


    }



    //径向模糊
    public IEnumerator RadialBlur(RadialBlurConfig config)
    {
        if (config != null)
        {
            radialBlur.active = config.active;

            float begin = GameTime.time;
            float end = GameTime.time + config.lerp;
            while (begin <= end)
            {
                begin += GameTime.deltaTime;
                var v = Mathf.Lerp(0, 0.106f, begin / end);
                radialBlur.intensity.value = config.active ? v : 0.106f - v;
                yield return new WaitForEndOfFrame();
            }

            if (config.active)
            {
                yield return new WaitForSeconds(0.5f);
                RadialBlurConfig quit = new RadialBlurConfig();
                quit.active = false;
                quit.lerp = 0.2f;
                DoRadialBlur(quit);
            }
        }

        RadiaBlurCoroutine = null;
    }



}
